/********************************** (C) COPYRIGHT *******************************
 * File Name          : ch32v30x_it.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2024/03/06
 * Description        : Main Interrupt Service Routines.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/
#include "main.h"
#include "eth_driver.h"

void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

void ETH_IRQHandler(void) __attribute__((interrupt()));
void TIM2_IRQHandler(void) __attribute__((interrupt()));
void EXTI9_5_IRQHandler(void) __attribute__((interrupt()));

/*********************************************************************
 * @fn      NMI_Handler
 *
 * @brief   This function handles NMI exception.
 *
 * @return  none
 */
void NMI_Handler(void)
{
  while (1)
  {
  }
}

/*********************************************************************
 * @fn      HardFault_Handler
 *
 * @brief   This function handles Hard Fault exception.
 *
 * @return  none
 */
void HardFault_Handler(void)
{
  printf("HardFault_Handler\r\n");

  printf("mepc  :%08x\r\n", __get_MEPC());
  printf("mcause:%08x\r\n", __get_MCAUSE());
  printf("mtval :%08x\r\n", __get_MTVAL());
  printf("reset\r\n");
  NVIC_SystemReset();
  while (1)
  {
  }
}

/*********************************************************************
 * @fn      EXTI9_5_IRQHandler
 *
 * @brief   This function handles GPIO exception.
 *
 * @return  none
 */
void EXTI9_5_IRQHandler(void)
{
  portDISABLE_INTERRUPTS();
  ETH_PHYLink();
  EXTI_ClearITPendingBit(EXTI_Line7); /* Clear Flag */
  portENABLE_INTERRUPTS();
}

/*********************************************************************
 * @fn      ETH_IRQHandler
 *
 * @brief   This function handles ETH exception.
 *
 * @return  none
 */
void ETH_IRQHandler(void)
{
  portDISABLE_INTERRUPTS();
  WCHNET_ETHIsr();
  portENABLE_INTERRUPTS();
}

// /*********************************************************************
//  * @fn      TIM2_IRQHandler
//  *
//  * @brief   This function handles TIM2 exception.
//  *
//  * @return  none
//  */
// void TIM2_IRQHandler(void)
// {
//   portDISABLE_INTERRUPTS();
//   WCHNET_TimeIsr(WCHNETTIMERPERIOD);
//   TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//   portENABLE_INTERRUPTS();
// }

// /*--------------------硬件压栈---------------------------------*/
// void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
// void SysTick_Handler( void )
// {
//     GET_INT_SP();
//     portDISABLE_INTERRUPTS();
//     SysTick->SR=0;
//     if( xTaskIncrementTick() != pdFALSE )
//     {
//         portYIELD();
//     }
//     portENABLE_INTERRUPTS();
//     FREE_INT_SP();
// }

/*--------------------软件压栈----------------------------------*/

uint32_t uwTick = 0;
void SysTick_Handler(void) __attribute__((interrupt()));
void SysTick_Handler(void)
{
  portDISABLE_INTERRUPTS();
  SysTick->SR = 0;
  uwTick++;
  if (xTaskIncrementTick() != pdFALSE)
  {
    portYIELD();
  }
  if (uwTick % 10 == 0)
    WCHNET_TimeIsr(WCHNETTIMERPERIOD);

  portENABLE_INTERRUPTS();
}